package com.classtwo.colleage.controler;

import com.classtwo.colleage.conf.*;
import com.classtwo.colleage.model.*;
import com.classtwo.colleage.service.iml.StudentServiceImpl;
import com.classtwo.colleage.tool.StringToTimeStamp;
import com.classtwo.colleage.vo.LeavePaperVo;
import com.classtwo.colleage.vo.PublictellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 主页页面控制器
 */
@Controller
@RequestMapping(value = "/Student")
public class StudentControler {

    @Autowired private StudentServiceImpl studentService;
    //防止出错
    private List<String> attendance = new ArrayList<>();
    private List<String> user =  new ArrayList<>();
    //学生注册
    @RequestMapping(value = "/register")
    public String RegisterStudent(Student student) {
        studentService.registerStudent(student);
        return "redirect:/index";
    }

    //学生登录界面
    @RequestMapping(value = "/login/StudentExam/loginUser")
    public String StudentLoginUser(HttpSession httpSession) {
        ModelAndView view = new ModelAndView();
        view.addObject("username",httpSession.getAttribute("username"));

        view.setViewName("/indexStudent.btl");

        Student student = studentService.studentLoginUser((String)httpSession.getAttribute("username"));
        httpSession.setAttribute("name",student.getName());
        httpSession.setAttribute("school",student.getschool());
        httpSession.setAttribute("id",student.getId());
        httpSession.setAttribute("studentnumber",student.getStudentnumber());
        return "redirect:/Student/login/ClassNext";
    }
    //注册账号重复检测
    @RequestMapping(value = "/login/StudentRegisterExam" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String, String> loginRegisterExam(@RequestBody Student data){
        Map<String,String> map=new HashMap<String, String>();
        map=studentService.loginRegisterExam(data);
        return map;
    }
    //登录检测
    @RequestMapping(value = "/login/StudentExam" , method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String, String> loginExam(@RequestBody Student student, HttpSession httpSession){
        Map<String,String> map=new HashMap<String, String>();
        map = studentService.loginExam(student);
        if(map.get("Answer").equals("TRUE")){
            httpSession.setAttribute("username",student.getUsername());
        }
        return map;
    }


    //刷新列表
    @RequestMapping(value = "/login/ClassNext")
    public ModelAndView StudentClassNext(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //自动查找相应的班级
        List<User_class> list= studentService.studentClassNext((String)httpSession.getAttribute("username"));
        /*    classInfo.findByUsername((String)httpSession.getAttribute("username"));*/
        view.setViewName("/indexStudent.btl" );
        view.addObject("classStudentList",list);
        view.addObject("username",httpSession.getAttribute("username"));
        return view;
    }

    //离开课堂
    @RequestMapping(value = "/login/StudentExam/loginUser/exitClass")
    public String StudentExitClass(String classnode,HttpSession httpSession){
        studentService.studentExitClass(classnode,(String)httpSession.getAttribute("username"));
        return "redirect:/Student/login/ClassNext";

    }

    //加入班级
    @RequestMapping(value = "/login/StudentExam/loginUser/enterClass", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Map<String,String> StudentEnterClass(String classnode,HttpSession httpSession){
        Map<String,String> map = new HashMap<>();
        map =studentService.studentEnterClass(classnode,(String)httpSession.getAttribute("username"),(String)httpSession.getAttribute("name"),(String) httpSession.getAttribute("studentnumber"),(String)httpSession.getAttribute("classnode"));
        return map;
    }

    //进入班级考勤页面
    @RequestMapping(value = "/login/StudentExam/loginUser/attendance")
    public ModelAndView jumpAttendance(String classnode,HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",classnode);
        view.addObject("page","attendance");
        //记录班级名
        User_class user_class =studentService.jumpAttendanceClassName(classnode);
        view.addObject("classname",user_class.getClassname());
        //记录班级邀请码
        httpSession.setAttribute("classnode",classnode);
        httpSession.setAttribute("classname",user_class.getClassname());

        //考勤数据获取
        //获取班级内学生全部人数
        List<User_class> list = studentService.jumpAttendanceList(classnode);
        //获取班级内所有考勤状态
        String all_data[][] = studentService.jumpAttendanceAll_data(list,(String)httpSession.getAttribute("classnode"));
        //获取班级内所有班级内的名字
        user = studentService.jumpAttendanceUser(list);
        //获取班级内所有班级内的学号
        String[] stuNumber = studentService.jumpAttendanceStuNum(list);
        //获取班级内所有班级内的考勤日期
        attendance =  studentService.jumpAttendanceAttendance(list,(String)httpSession.getAttribute("classnode"));

        view.setViewName("/studentcontent1.btl");
        view.addObject("user",user);
        view.addObject("attendance",attendance);
        view.addObject("stuNumber",stuNumber);
        view.addObject("all_data",all_data);
        return view ;
    }

    //成员页面
    @RequestMapping(value = "/login/StudentExam/loginUser/jump/member")
    public ModelAndView jumpmember(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("page","member");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        //班级所有信息
        List<User_class> allClass = studentService.jumpMember((String)httpSession.getAttribute("classnode"));
        /*classInfo.findAllByUsertypeAndClassnode("学生",(String)httpSession.getAttribute("classnode"));*/
        view.addObject("user",user);
        view.addObject("attendance",attendance);
        view.addObject("studentlist",allClass);
        view.setViewName("/studentcontent1.btl");
        return view ;
    }
    //通知页面
    @RequestMapping(value = "/login/StudentExam/loginUser/jump/publictell")
    public ModelAndView jumppublictell(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("page","publictellPage");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        //功能信息
        List<Publictell> publictells = studentService.jumpPublicTell((String)httpSession.getAttribute("classnode"));
        view.addObject("user",user);
        view.addObject("attendance",attendance);
        view.addObject("tellList",publictells);
        view.setViewName("/studentcontent1.btl");
        return view ;
    }


    //特殊情况页面
    @RequestMapping(value = "/login/StudentExam/loginUser/jump/specialcase")
    public ModelAndView jumpSpecialcase(HttpSession httpSession){
        ModelAndView view = new ModelAndView();
        //用户相关信息
        view.addObject("page","specialcase");
        view.addObject("username",httpSession.getAttribute("username"));
        view.addObject("classnode",httpSession.getAttribute("classnode"));
        view.addObject("classname",httpSession.getAttribute("classname"));
        view.addObject("user",user);
        view.addObject("attendance",attendance);
        //功能信息
        Student student = studentService.jumpSpecialcase((String)httpSession.getAttribute("username"));
        view.addObject("student",student);
        view.setViewName("/studentcontent1.btl");
        return view ;
    }

    //提交请假条页面
    @RequestMapping(value = "/saveLeavePaper")
    public String SaveLeavePaper(HttpSession httpSession,String leave_byone,String leave_byone_number,String leave_info,String start_time,String end_time,String creat_time,String teacherName){
        studentService.saveLeavePaper((String)httpSession.getAttribute("classnode"),leave_byone,leave_byone_number,leave_info,start_time,end_time,creat_time,teacherName);
        return "redirect:/Student/login/StudentExam/loginUser/jump/specialcase";
    }
    //查看请假条记录
    @RequestMapping(value = "/findStuLeaveList", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public LeavePaperVo findStuLeaveList (HttpSession httpSession){
        LeavePaperVo leavePaperVos = new LeavePaperVo();
        leavePaperVos=studentService.findStuLeaveList((String)httpSession.getAttribute("username"),(String)httpSession.getAttribute("classnode"));
        return leavePaperVos;
    }
}

